home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ARASAN_S.ZIP / LOG.CPP < prev    next >
C/C++ Source or Header  |  1994-08-06  |  5KB  |  226 lines

  1. // Copyright 1994 by Jon Dart.  All Rights Reserved.
  2.  
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <windows.h>
  6. #include <assert.h>
  7. #include "log.h"
  8. #include "notation.h"
  9.  
  10. #define LOG_FILE_NAME "ARASAN.LOG"
  11. #define MAX_ANAL 5
  12.  
  13. Log_Entry::Log_Entry(const ReversibleMove &move,
  14.   const char *move_image)
  15. : my_move(move),
  16.   my_image(new char[strlen(move_image)+1])
  17. {
  18.   strcpy(my_image,move_image);
  19. }
  20.  
  21. Log_Entry::Log_Entry()
  22. : my_move(ReversibleMove()),my_image(NULL)
  23. {
  24. }
  25.  
  26. Log_Entry::Log_Entry( const Log_Entry &le)
  27. {
  28.      my_move = le.my_move;
  29.      my_image = new char[strlen(le.my_image)+1];
  30.      assert(my_image);
  31.      strcpy(my_image,le.my_image);
  32. }
  33.          
  34. Log_Entry & Log_Entry::operator = (const Log_Entry &le)
  35. {
  36.    if (&le != this)
  37.    {
  38.        my_move = le.my_move;
  39.        delete [] my_image;
  40.        my_image = new char[strlen(le.my_image)+1];
  41.        assert(my_image);
  42.        strcpy(my_image,le.my_image);
  43.    }
  44.    return *this;
  45. }
  46.  
  47. Log_Entry::~Log_Entry()
  48. {
  49.    delete [] my_image;    
  50. }
  51.  
  52. static char *time_image(const time_t time)
  53. {
  54.    static char buf[80];
  55.    int hrs = time / 3600;
  56.    int mins = (time - (hrs*3600))/60;
  57.    int secs = time - (hrs*3600) - (mins*60);
  58.    wsprintf(buf,"%02d:%02d:%02d",hrs,mins,secs);
  59.    return buf;
  60. }
  61.  
  62. Log::Log() : Array<Log_Entry>(Initial_Log_Size,False)
  63. {
  64.    my_current = 0;
  65.    *buf = '\0';
  66.    log_file.open((LPSTR)LOG_FILE_NAME,ios::out|ios::trunc);
  67.    if (!log_file.good())
  68.    {
  69.       MessageBox(NULL,"Can't open log file.  Game moves will not be saved.","",MB_OK);
  70.    }
  71. }
  72.          
  73. Log::~Log()
  74. {
  75.    if (log_file.good())
  76.    {
  77.       log_file << buf << endl;
  78.       log_file.close();
  79.    }
  80. }
  81.  
  82. void Log::add_move( Board &board, const ReversibleMove &emove,
  83.  const char *move_image, const Search::Statistics *stats,
  84.  const Boolean toFile )
  85. {
  86.     Log_Entry entry( emove, move_image );
  87.     // moves are always added at the "current" position.
  88.     resize(current());
  89.     *this += entry;
  90.     my_current++;
  91.     if (!toFile)
  92.        return;
  93.     char num[10];
  94.     *buf = '\0';
  95.     wsprintf(num,"%d. ",(num_moves()-1)/2 + 1);
  96.     strcat(buf,num);
  97.     strcat(buf,move_image);
  98.     int len = strlen(buf);
  99.     char *p = buf + len;
  100.     for (int i = 0; i < 15-len; i++)
  101.        *p++ = ' ';
  102.     *p = '\0';
  103.     if (stats)
  104.     {
  105.         char statbuf[80];
  106.     if (stats->num_moves == 0)
  107.        strcpy(statbuf," (book)");
  108.     else
  109.     {
  110.        char pred[50];
  111.        *pred = '\0';
  112.        Board tmp_board = board;
  113.            tmp_board.MakeMove(emove);
  114.        for (int i=1; i<=MAX_ANAL; i++)
  115.        {
  116.               Move m = stats->best_line[i];
  117.           if (m.IsNull())
  118.              break;
  119.           char pred_move_image[20];
  120.               *pred_move_image = '\0';
  121.           Notation::Image(tmp_board,m,pred_move_image);
  122.           strcat(pred,pred_move_image);
  123.           strcat(pred," ");
  124.           tmp_board.MakeMove(ExtendedMove(tmp_board,m));
  125.        }
  126.            wsprintf(statbuf,"\t%s    %d\t%ld\t%ld\t%5d\t%s",
  127.           time_image(stats->elapsed_time),
  128.           stats->plies_completed,
  129.           stats->num_moves,
  130.           stats->num_pos,
  131.           stats->value,pred);
  132.     }
  133.         if (stats->state == Search::Checkmate)
  134.            strcat(statbuf," mate");
  135.     else if (stats->state == Search::Stalemate)
  136.        strcat(statbuf," stalemate");
  137.     else if (stats->state == Search::Resigns)
  138.        strcat(statbuf," resigns");
  139.     else if (stats->state == Search::Draw)
  140.        strcat(statbuf," draw");
  141.     strcat(buf,statbuf);
  142.     }
  143.     log_file << buf << endl;
  144.     *buf = '\0';
  145. }
  146.  
  147. const ReversibleMove &Log::move( const unsigned n ) const
  148. {
  149.     return (*this)[n].move();
  150. }
  151.  
  152. const ReversibleMove &Log::last_move() const
  153. {
  154.     assert (current());
  155.     return move(current()-1);
  156. }
  157.     
  158. void Log::remove_move()
  159. {
  160.     if (current() == 0)
  161.        return;
  162.     resize(current()-1);
  163.     --my_current;
  164. }
  165.  
  166. Boolean Log::back_up()
  167. {
  168.     if (my_current >0)
  169.     {
  170.        --my_current;
  171.        return True;
  172.     }
  173.     else
  174.        return False;
  175. }
  176.  
  177. Boolean Log::go_forward()
  178. {
  179.     if (my_current < size())
  180.     {
  181.        ++my_current;
  182.        return True;
  183.     }
  184.     else
  185.        return False;
  186. }
  187.  
  188. void Log::reset()
  189. {
  190.     my_current = 0;
  191. }
  192.  
  193. void Log::clear()
  194. {
  195.      write_eol();
  196.      resize(0);
  197.      my_current = 0;
  198. }
  199.  
  200. void Log::write_header()
  201. {
  202.      flush();
  203.      static char header[] = "   move          time     depth\tmoves\tnodes\tvalue\tpredicted";
  204.      log_file << header << endl;
  205. }
  206.  
  207. void Log::write(char *s)
  208. {
  209.      flush();
  210.      log_file << s;
  211. }
  212.  
  213. void Log::write_eol()
  214. {
  215.      log_file << endl;
  216. }
  217.  
  218. void Log::flush()
  219. {
  220.      if (strlen(buf) > 0)
  221.      {    
  222.         log_file << buf << endl;
  223.      }
  224.      *buf = '\0';
  225. }
  226.